//
//  SWCameraSoundBar.swift
//  ChildStory
//
//  Created by linhan on 2016/11/10.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit


fileprivate struct SoundItem
{
    var name:String = ""
    var iconName:String
    var selectedIconName:String
    var sound:SWSoundFile
    
    init(iconName:String, selectedIconName:String, sound:SWSoundFile) {
        self.iconName = iconName
        self.selectedIconName = selectedIconName
        self.sound = sound
    }
}

class SWCameraSoundBar: UIView,UICollectionViewDataSource,UICollectionViewDelegate
{
    
    var selectedIndex:Int = -1 {
        didSet{
            
        }
    }
    
    private var _sounds:[SoundItem] = []
    
    private var _collectionView:UICollectionView?
    
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        _sounds.append(SoundItem(iconName: "editor_camera_icon_cat", selectedIconName:"editor_camera_icon_catHL", sound: SWSoundFile.cat))
        _sounds.append(SoundItem(iconName: "editor_camera_icon_bird", selectedIconName:"editor_camera_icon_birdHL", sound: SWSoundFile.bird))
        _sounds.append(SoundItem(iconName: "editor_camera_icon_koala", selectedIconName:"editor_camera_icon_koalaHL", sound: SWSoundFile.koala))
        _sounds.append(SoundItem(iconName: "editor_camera_icon_duck", selectedIconName:"editor_camera_icon_duckHL", sound: SWSoundFile.duck))
        _sounds.append(SoundItem(iconName: "editor_camera_icon_ring", selectedIconName:"editor_camera_icon_ringHL", sound: SWSoundFile.ring))
        _sounds.append(SoundItem(iconName: "editor_camera_icon_drum", selectedIconName:"editor_camera_icon_drumHL", sound: SWSoundFile.drum))
        _sounds.append(SoundItem(iconName: "editor_camera_icon_flute", selectedIconName:"editor_camera_icon_fluteHL", sound: SWSoundFile.flute))
        
        
        let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake(33, height)
        layout.minimumLineSpacing = 22
        layout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12)
        layout.scrollDirection = .horizontal
        
        
        _collectionView = UICollectionView(frame: bounds, collectionViewLayout: layout)
        _collectionView?.backgroundColor = UIColor.clear
        _collectionView?.register(SWSoundCollectionCell.self, forCellWithReuseIdentifier: "SWSoundCollectionCell")
        _collectionView?.dataSource = self
        _collectionView?.delegate = self
        addSubview(_collectionView!)
    }
    
    //取消选中
    func deselect()
    {
        if let indexPath = _collectionView?.indexPathsForSelectedItems?.valueAt(0)
        {
            _collectionView?.deselectItem(at: indexPath, animated: false)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return _sounds.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell:SWSoundCollectionCell? = collectionView.dequeueReusableCell(withReuseIdentifier: "SWSoundCollectionCell", for: indexPath) as? SWSoundCollectionCell
        cell?.sound = _sounds.valueAt(indexPath.row)
        return cell ?? UICollectionViewCell()
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        let index:Int = indexPath.row
        if let sound = _sounds.valueAt(index)
        {
            SWSoundPlayer.play(file: sound.sound, times: 3)
        }
    }
    
    
}


fileprivate class SWSoundCollectionCell: UICollectionViewCell
{
    var sound:SoundItem? {
        didSet {
            if _change
            {
                layoutSubviews()
            }
        }
    }
    
    private var _change:Bool = true
    
    private var _inited:Bool = false
    
    private var _label:UILabel = UILabel()
    private var _imageView:UIImageView = UIImageView()
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setup()
    }
    
    deinit
    {
        //trace("DEINIT SWAssetCollectionCell")
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        //contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        _imageView.frame = bounds
        _imageView.contentMode = .center
        addSubview(_imageView)
    }
    
    override var isSelected:Bool{
        get{
            return super.isSelected
        }
        set{
            super.isSelected = newValue
            _imageView.image = UIImage(named: (isSelected ? sound?.selectedIconName : sound?.iconName) ?? "")
        }
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let sound = sound , _change
        {
            _change = false
            _imageView.image = UIImage(named: isSelected ? sound.selectedIconName : sound.iconName)
        }
    }
    
    
}



